* Load Compustat data
sysuse comp_ann, clear
keep gvkey fyear lpermno sich prcc_f csho at ceq sale dlc dltt
rename fyear year
rename lpermno permno

* Merge with patent data
merge 1:1 permno year using patents
rename year fyear
drop if _merge==2 
drop _merge

* Computing industry definitions
bys gvkey: egen sic=median(sich)
replace sic=floor(sic)

xtset gvkey fyear

g size=log(at)
g mktCap=prcc_f*csho
g quantityRaw=fnpats
g valueRaw=tsm/l.mktCap

drop if sic==.
drop if (sic>=9000) | ((sic>=4900) & (sic<=4999)) | ((sic>=6000) & (sic<=6999))

winsor2 quantityRaw valueRaw, replace cuts(2.5 97.5) by(fyear)

* Data for Figure 1
egen sizeQ = xtile(size), n(10) by(fyear)
tabstat quantityRaw valueRaw, by(sizeQ) nototal 
preserve
collapse (mean) quantityRaw valueRaw, by(sizeQ)
export delimited "size.txt", delim(tab) replace
restore